package com.netik.getdata;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import android.content.Context;

import com.netik.main.R;

public class GetDatanew {

	private int i=0;
	public String[] enq_id; 
	public String[] enq_name; 
	public String[] enq_val; 
	//public String[] stock_price = new String[100];
	public int ctr=0;
	  public void examineXMLFile(Context context)
	    {
		  try {
			  InputStream is = context.getResources().openRawResource(R.raw.sample1);
	            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	            Document doc = docBuilder.parse (is);

	            // normalize text representation
	            doc.getDocumentElement ().normalize ();
	            System.out.println ("Root element of the doc is " + 
	                 doc.getDocumentElement().getNodeName());

	            
	            NodeList listOfDocs = doc.getElementsByTagName("Table");
	            int totalDocs = listOfDocs.getLength();
	            System.out.println("Total no of results : " + totalDocs);
	            int s;
	            ctr=totalDocs;
	            
	            enq_id = new String[ctr+1];
	            enq_name = new String[ctr+1];
	            enq_val = new String[ctr+1];
	            
	            for(s=0; s<listOfDocs.getLength() ; s++){


	                Node firstPersonNode = listOfDocs.item(s);
	                if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


	                    Element firstPersonElement = (Element)firstPersonNode;

	                    //-------
	                    NodeList firstNameList = firstPersonElement.getElementsByTagName("EV_CREATED_TMS");
	                    Element firstNameElement = (Element)firstNameList.item(0);

	                    NodeList textFNList = firstNameElement.getChildNodes();
	                   // System.out.println("EV_CREATED_TMS : " + 
	                       //    ((Node)textFNList.item(0)).getNodeValue().trim());
	                    enq_id[s] = ((Node)textFNList.item(0)).getNodeValue().trim().toString();

	                    //-------
	                    NodeList lastNameList = firstPersonElement.getElementsByTagName("QUANTITY");
	                    Element lastNameElement = (Element)lastNameList.item(0);

	                    NodeList textLNList = lastNameElement.getChildNodes();
	                    //System.out.println("QUANTITY : " + 
	                       //    ((Node)textLNList.item(0)).getNodeValue().trim());
	                    enq_name[s] = ((Node)textLNList.item(0)).getNodeValue().trim().toString();

	                    //----
	                    NodeList ageList = firstPersonElement.getElementsByTagName("TRN_DESC");
	                    Element ageElement = (Element)ageList.item(0);

	                    NodeList textAgeList = ageElement.getChildNodes();
	                   // System.out.println("TRN_DESC : " + 
	                         //  ((Node)textAgeList.item(0)).getNodeValue().trim());
	                    enq_val[s] = ((Node)textAgeList.item(0)).getNodeValue().trim().toString();

	                    //------
/*
	                    NodeList numList = firstPersonElement.getElementsByTagName("PRCD_ALT_CMB_AMT");
	                    Element numElement = (Element)numList.item(0);

	                    NodeList textnumList = numElement.getChildNodes();
	                    System.out.println("PRCD_ALT_CMB_AMT : " + 
	                           ((Node)textnumList.item(0)).getNodeValue().trim());
*/
	                    //------

	                }//end of if clause


	            }//end of for loop with s var
	            	

	        }catch (SAXParseException err) {
	        System.out.println ("** Parsing error" + ", line " 
	             + err.getLineNumber () + ", uri " + err.getSystemId ());
	        System.out.println(" " + err.getMessage ());

	        }catch (SAXException e) {
	        Exception x = e.getException ();
	        ((x == null) ? e : x).printStackTrace ();

	        }catch (Throwable t) {
	        t.printStackTrace ();
	        }
	    }
}
